package com.jiuyue.movehouse.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jiuyue.movehouse.common.exception.BadRequestException;
import com.jiuyue.movehouse.entity.ExtInfo;
import com.jiuyue.movehouse.mapper.ExtInfoMapper;
import com.jiuyue.movehouse.service.ExtInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yourFather
 * @since 2022-02-11
 */
@Service
public class ExtInfoServiceImpl extends ServiceImpl<ExtInfoMapper, ExtInfo> implements ExtInfoService {

    @Override
    public List<ExtInfo> getExtInfoList() {
        return this.list();
    }

    @Override
    public void saveExtInfo(ExtInfo extInfo) {
        checkName(extInfo);
        this.save(extInfo);
    }

    @Override
    public void updateExtInfo(ExtInfo extInfo) {
        checkName(extInfo);
        this.updateById(extInfo);
    }

    @Override
    public void deleteExtInfo(Long extInfoId) {
        this.removeById(extInfoId);
    }

    private void checkName(ExtInfo extInfo){
        if (this.count(Wrappers.<ExtInfo>lambdaQuery().eq(ExtInfo::getName,extInfo.getName())
        .ne(Objects.nonNull(extInfo.getId()),ExtInfo::getId,extInfo.getId()))>0){
            throw new BadRequestException(extInfo.getName()+"已存在");
        }
    }
}
